package com.hawthorn.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>todo 发送一次消息，多个符合的队列都可以收到消息，发送者和消费者一对多</p>
 * @author: andy.ten@tom.com | xukaixing@hotmail.com
 * @created: 2022/7/10 09:30
 * @version v1.0.2
 * @copyright: Copyright (c)  andyten
 */
@Configuration
public class TopicRabbitConfig
{
  //绑定键
  // public final static String man = "topic.man";
  //
  // public final static String woman = "topic.women";

  public final static String logLogin = "hawthorn.topic.logs.login";
  public final static String operationLogin = "hawthorn.topic.logs.operation";

  @Bean
  public Queue logLoginQueue()
  {
    return new Queue(logLogin);
  }

  @Bean
  public Queue logOperationQueue()
  {
    return new Queue(operationLogin);
  }
  // @Bean
  // public Queue firstQueue()
  // {
  //   return new Queue(TopicRabbitConfig.man);
  // }
  //
  // @Bean
  // public Queue secondQueue()
  // {
  //   return new Queue(TopicRabbitConfig.woman);
  // }

  //将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
  //这样只要是消息携带的路由键是topic.man,才会分发到该队列
  // @Bean
  // Binding bindingExchangeMessage()
  // {
  //   return BindingBuilder.bind(firstQueue()).to(exchange()).with(man);
  // }

  //将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
  // 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
  // @Bean
  // Binding bindingExchangeMessage2()
  // {
  //   return BindingBuilder.bind(secondQueue()).to(exchange()).with("topic.");
  // }

  // TopicExchange创建的交换机默认是持久化的
  @Bean
  TopicExchange exchangeLogLogin()
  {
    return new TopicExchange("hawthorn.topic.logLogin");
  }

  @Bean
  TopicExchange exchangeLogOperation()
  {
    return new TopicExchange("hawthorn.topic.logOperation");
  }

  // 登录日志队列
  @Bean
  Binding bindingExchangeMessageLogin()
  {
    return BindingBuilder.bind(logLoginQueue()).to(exchangeLogLogin()).with(logLogin);
  }

  @Bean
  Binding bindingExchangeMessageOperation()
  {
    return BindingBuilder.bind(logOperationQueue()).to(exchangeLogOperation()).with(operationLogin);
  }

}
